<template>
    <div class="nav">
        <!-- @click 中 只可以访问当前组件实例上挂载的属性和方法 -->
        <div :class="{'nav-item':true,active:currentIndex===index}" v-for="(item,index) in navList" :key="index" @click="saveIndex(index)">
            {{item}}
        </div>
    </div>
</template>

<script>
import vm from '../../vm'

export default {
    data(){
        return {
            currentIndex:0,//保存当前点击 导航项的索引
            navList:[
                '价格升序↑',
                '价格降序↓',
                '销量升序↑',
                '销量降序↓'
            ]
        }
    },
    methods:{
        /* 注意: methods中的方法不能是箭头方法 */
        saveIndex: function(index){
            this.currentIndex = index;
            vm.$emit('receiveIndex',this.currentIndex);
        }
    }
}
</script>
<style scoped>
    .nav{
        display: flex;
        justify-content: space-between;
        align-items: center;
        line-height: 50px;
        margin: 0 20px;
        border-bottom: 1px solid #f5f5f5;
    }
    .active{
        color: red;
    }
</style>